<?php


namespace App\Http\Controllers\Api;


use App\Http\Controllers\Controller;
use App\Models\CommodityOrder;
use Illuminate\Http\Request;

class AftermarketController extends Controller
{

    public function refund(Request $request)
    {
        $internal_order_sn = $request->input('internal_order_sn');

        $order = CommodityOrder::where('internal_order_sn', $internal_order_sn)->first();
        if (!$order) {
            return $this->makeResponse([], '订单错误', 404);
        }
        if ($order->order_status == 1) {
            $order->order_status = 6;
        } elseif ($order->order_status == 2) {
            //此处应该做企业退费
            $order->is_refund = 1;
        } else {
            return $this->makeResponse([], '订单错误', 404);
        }

        $order->save();
        return $this->makeResponse([], '取消成功', 200);
    }

    public function rejected(Request $request)
    {
        $internal_order_sn = $request->input('internal_order_sn');
        $order = CommodityOrder::with('shop')->where(function ($query) {
            $query->where('order_status', 4)->orWhere('order_status', 5);
        })->where('internal_order_sn', $internal_order_sn)->first();

        if (!$order) {
            return $this->makeResponse([], '订单错误', 404);
        }

        return $this->makeResponse($order, '查询成功', 200);
    }
}